using System;
using System.Collections.Generic;
using System.Text;
using MacomberMap.Common.Types;
using System.Xml;
using System.Data.Common;
using MacomberMap.Common.User_Interfaces.Components;
using System.ComponentModel;

namespace MacomberMap.Common.Components
{
    /// <summary>
    /// This class holds the information on transformers
    /// </summary>
    [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.Both)]
    public class MM_Transformer: MM_Element
    {
        #region Variable Declarations
        private MM_TransformerWinding[] _Windings = new MM_TransformerWinding[2];


        /// <summary>
        /// The transformer windings involved with this transformer
        /// </summary>
        [Category("Element"), Description("The transformer windings involved with this transformer")]
        public MM_TransformerWinding[] Windings
        {
            get { return _Windings; }
            set { _Windings = value; }
        }

        /// <summary>
        /// Whether the secondary winding is considered to be a phase shifter
        /// </summary>
        [Category("Element"), Description("Whether the secondary winding is considered to be a phase shifter"), DefaultValue(false), MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public bool PhaseShifter
        {
            get { return _PhaseShifter; }
            set { _PhaseShifter = value; }
        }
        private bool _PhaseShifter = false;

        /// <summary>The first KV Level</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public MM_KVLevel KVLevel1
        {
            get { return Windings[0].KVLevel; }
            set
            {
                if (Windings[0] == null)
                {
                    Windings[0] = new MM_TransformerWinding();                        
                    Windings[0].ElemType = MM_Overall_Display.FindElementType("TransformerWinding");
                }
                Windings[0].KVLevel = value; }
        }
        
        /// <summary>The second KV Level</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public MM_KVLevel KVLevel2
        {
            get { return Windings[1].KVLevel; }
            set
            {                
                if (Windings[1] == null)
                {
                    Windings[1] = new MM_TransformerWinding();
                    Windings[1].ElemType = MM_Overall_Display.FindElementType("TransformerWinding");
                }
                Windings[1].KVLevel = value;
            }
        }

        /// <summary>The first KV Level</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public MM_TransformerWinding Winding1
        {
            get { return Windings[0]; }
            set { Windings[0] = value; }
        }

        /// <summary>The second KV Level</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.TypeCollection)]
        public MM_TransformerWinding Winding2
        {
            get { return Windings[1]; }
            set { Windings[1] = value; }
        }

        /// <summary>Override the KV Level, so it's not stored</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.None)]
        public new MM_KVLevel KVLevel
        {
            get { return null; }
        }
        #endregion


        #region Initialization
        /// <summary>
        /// Initialize a new CIM Transformer
        /// </summary>
        /// <param name="ElementSource">The XML source for this line</param>
        public MM_Transformer(XmlElement ElementSource)
            : base(ElementSource)
        { 
            this.ElemType = MM_Overall_Display.ElemTypes["Transformer"];
            String[] splStr = ElementSource.Attributes["Windings"].Value.Split(',');
            _Windings = new MM_TransformerWinding[splStr.Length / 2];
            for (int a = 0; a < splStr.Length; a += 2)
            {
                _Windings[a / 2] = new MM_TransformerWinding();
                _Windings[a / 2].ElemType = MM_Overall_Display.ElemTypes["Transformer"];
                _Windings[a / 2].KVLevel = MM_Overall_Display.KVLevels[splStr[a + 1]];
                _Windings[a / 2].Name = this.Name + "(" + ((a / 2) + 1).ToString() + ")";
                _Windings[a / 2].Operator = this.Operator;
                _Windings[a / 2].Owner = this.Owner;
                _Windings[a / 2].Substation = this.Substation;
                _Windings[a / 2].TEID = UInt64.Parse(splStr[a]);
                _Windings[a / 2].Transformer = this;
            }

        
        
        }


        /// <summary>
        /// Instantiate a new transformer element from the CIM Server
        /// </summary>
        /// <param name="ElementSource">The data row for the element</param>
        public MM_Transformer(DbDataReader ElementSource)
            : base(ElementSource)
        {
            //Pull in the windings
            for (int a=0; a < 2; a++)
            {
                _Windings[a] = new MM_TransformerWinding();
                if (ElementSource["WINDING" + (a+1).ToString()] is DBNull == false)
                    _Windings[a].TEID = Convert.ToUInt64(ElementSource["WINDING" + (a+1).ToString()]);
                _Windings[a].Name = this.Name + " (" + a.ToString() + ")";
                _Windings[a].ElemType = MM_Overall_Display.ElemTypes["Transformer"];
                _Windings[a].Operator = this.Operator;
                _Windings[a].Owner= this.Owner;
                _Windings[a].Substation= this.Substation;
                _Windings[a].Transformer = this;
                foreach (MM_KVLevel KVLevel in MM_Overall_Display.KVLevels.Values)
                    if (KVLevel.Index == Convert.ToInt32(ElementSource["KVLEVEL" + (a+1).ToString()]))
                        _Windings[a].KVLevel = KVLevel;
            }
            this.ElemType = MM_Overall_Display.ElemTypes["Transformer"];

        }


        /// <summary>
        /// Initialize a blank transformer
        /// </summary>
        public MM_Transformer()
        { this.ElemType = MM_Overall_Display.ElemTypes["Transformer"]; }
        #endregion
    }
}
